package com.idbk.chargestation.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.charge.ActivityChargingAc;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.bean.JsonQrScan;

import java.util.Locale;

/**
 * 按金额的自定义充电
 *
 * @author zhx
 */

public class FragmentChargeByCost extends Fragment implements OnClickListener {

    public View mView;
    private TextView mHundred, mEighty, mFifty, mTwenty, mOther, mBalance;
    private EditText mInputMoney;
    private TextView[] mTextMoneyArrays = new TextView[5];
    private int mSelectMoney = 100;
    public boolean isChoose = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_charge_cost, container, false);

        setupView();

        setupData();

        return mView;
    }

    private void setupView() {
        mHundred = (TextView) mView.findViewById(R.id.hundred);
        mEighty = (TextView) mView.findViewById(R.id.eighty);
        mFifty = (TextView) mView.findViewById(R.id.fivty);
        mTwenty = (TextView) mView.findViewById(R.id.twenty);
        mBalance = (TextView) mView.findViewById(R.id.balance);
        mOther = (TextView) mView.findViewById(R.id.other);
        mInputMoney = (EditText) mView.findViewById(R.id.money);
        mView.findViewById(R.id.start).setOnClickListener(this);

        mTextMoneyArrays[0] = mHundred;
        mTextMoneyArrays[1] = mEighty;
        mTextMoneyArrays[2] = mFifty;
        mTextMoneyArrays[3] = mTwenty;
        mTextMoneyArrays[4] = mOther;
        for (int i = 0; i < mTextMoneyArrays.length; i++) {
            mTextMoneyArrays[i].setOnClickListener(this);
        }
    }

    private void setupData() {
        JsonQrScan mPointInfor = getArguments().getParcelable(JsonQrScan.KEY);

        SpannableString spanHundred, spanEighty, spanFifty, spanTwenty;

        spanHundred = new SpannableString("100元");
        spanHundred.setSpan(new AbsoluteSizeSpan(14, true), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanHundred.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_0)), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanHundred.setSpan(new AbsoluteSizeSpan(12, true), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanHundred.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mHundred.setText(spanHundred);

        spanEighty = new SpannableString("80元");
        spanEighty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mEighty.setText(spanEighty);

        spanFifty = new SpannableString("50元");
        spanFifty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanFifty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanFifty.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanFifty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mFifty.setText(spanFifty);

        spanTwenty = new SpannableString("20元");
        spanTwenty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTwenty.setText(spanTwenty);

        if (mPointInfor != null) {
            mBalance.setText(String.format(Locale.CHINA, "%.2f", mPointInfor.balance));
        }
    }

    private void setMoneySelect(int index) {
        for (int i = 0; i < mTextMoneyArrays.length; i++) {
            if (i == index) {
                mTextMoneyArrays[i].setBackgroundResource(R.drawable.bg_select_box_active_sprite);
            } else {
                mTextMoneyArrays[i].setBackgroundResource(R.drawable.bg_select_box_active_disabled);
            }

            if (index == mTextMoneyArrays.length - 1) {
                mInputMoney.setBackgroundResource(R.drawable.bg_textfield_normal_sprite);
                mInputMoney.setFocusableInTouchMode(true);
                isChoose = true;
            } else {
                mInputMoney.setFocusable(false);
                mInputMoney.setText("");
                mInputMoney.setBackgroundResource(R.drawable.bg_textfield_disabled_sprite);
                isChoose = false;
            }
        }
    }

    private boolean checkInput() {
        if (isChoose) {
            String money = mInputMoney.getText().toString().trim();
            if (money.equals("") || money.equals("")) {
                Snackbar.make(mInputMoney, "请输入正确的数值！", Snackbar.LENGTH_SHORT).show();
                return false;
            } else {
                int value = Integer.parseInt(money);
                if (value > 9999 || value < 0) {
                    Snackbar.make(mInputMoney, "请输入0-9999的整数金额（元）！", Snackbar.LENGTH_SHORT).show();
                    return false;

                } else {
                    mSelectMoney = value;
                    return true;
                }
            }
        }
        return true;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.hundred:
                setMoneySelect(0);
                mSelectMoney = 100;
                break;
            case R.id.eighty:
                setMoneySelect(1);
                mSelectMoney = 80;
                break;
            case R.id.fivty:
                setMoneySelect(2);
                mSelectMoney = 50;
                break;
            case R.id.twenty:
                setMoneySelect(3);
                mSelectMoney = 20;
                break;
            case R.id.other:
                setMoneySelect(4);
                break;
            case R.id.start:
                if (checkInput()) {
                    System.out.println("mSelectMoney-->" + mSelectMoney);
                    Intent intent = getActivity().getIntent();
                    intent.putExtra("mMethod", JsonPointPile.CHARGE_FINISH_BY_MONEY);
                    intent.putExtra("mSelect", mSelectMoney);
                    intent.putExtra(ActivityChargingAc.KEY_CHARGE_ACTION, ActivityChargingAc.CHARGE_ACTION_WAIT);
                    getActivity().setResult(Activity.RESULT_OK, intent);
                    getActivity().finish();
                }
                break;
            default:
                break;
        }
    }

}
